package com.ljxt.dt.web.servlet.purchasingmanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.HandlerInfo;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseDetailService;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseInfoService;
import com.ljxt.dt.service.purchasingmanagementservice.impl.PurchaseDetailServiceImpl;
import com.ljxt.dt.service.purchasingmanagementservice.impl.PurchaseInfoServiceImpl;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.util.xhq.ClassUtil;
import com.ljxt.dt.util.xhq.JdbcUtils;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 徐浩钦
 * @version 1.0.0
 * @date 2020/5/19 11:30
 * @copyright 老九学堂T11
 */
@WebServlet("/xhqCgddUpdata")
public class CgddUpdataServlet extends HttpServlet {


    private static PurchaseInfoService purchaseInfoService = new PurchaseInfoServiceImpl();
    private static PurchaseDetailService purchaseDetailService = new PurchaseDetailServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderNo = req.getParameter("orderNo");

        req.setAttribute("orderNo",orderNo );
        req.getRequestDispatcher("/WEB-INF/views/purchasingManagement/cgddUpdata.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("执行了 CgddUpdataServlet -> doPost");

        String orderNo = req.getParameter("orderNo");
        System.out.println(orderNo);

        HandlerInfo handlerInfo = purchaseInfoService.selectObjByCgdd(orderNo);

        // 如果没有改审核流程则 创建一个新的审核流程 为 1 ， 1 的
        if(handlerInfo == null){
            handlerInfo = new HandlerInfo();
            handlerInfo.setOrderNo(orderNo);
            handlerInfo.setName("徐浩强");
            handlerInfo.sethandlerTypeId(1);
            handlerInfo.setResultId(1);
            handlerInfo.setTime(new Date());
            handlerInfo.setRemarks("测试备注");

        }else{
            // 如果存在了还在编辑界面点击了提交那就按照添加
            handlerInfo.sethandlerTypeId(1);
            handlerInfo.setId(null);
            handlerInfo.setResultId(1);
            handlerInfo.setTime(new Date());
            handlerInfo.setRemarks("再次提交");
        }


        // 添加一个记录
        int i = purchaseInfoService.insertIntoHandlerInfo(handlerInfo);
        JsonResult jsonResult = new JsonResult();
        // 如果受影响行数 > 0 则
        if (i > 0){
            jsonResult.setCode("200");
        }else{
            jsonResult.setCode("500");
        }

        resp.getWriter().write(new ObjectMapper().writeValueAsString(jsonResult));
    }
}
